package com.java.service.impl;


import com.java.dao.GoodsDao;
import com.java.dao.impl.GoodsDaoImpl;
import com.java.domain.Goods;
import com.java.service.GoodsService;
import com.java.utils.JedisUtil;
import com.java.utils.SerializeUtil;
import redis.clients.jedis.Jedis;
import redis.clients.util.SafeEncoder;

import java.util.List;

public class GoodServiceImpl implements GoodsService {
    private GoodsDaoImpl gd = new GoodsDaoImpl();

    private JedisUtil jedisUtil= JedisUtil.getInstance();//实例化工具类
    private Jedis jedis=JedisUtil.getInstance().getJedis();//从中获取Jedis实例
    private JedisUtil.Strings strings=jedisUtil.new Strings();//从中获取Strings字符串类实例
    @Override
    public List<Goods> getAllGoods() {

        //现在取出
        byte[] trainByte = jedis.get(SafeEncoder.encode("MYSCGoodsCache"));
        //反序列化返回对象
        List goodsList = (List<Goods>) SerializeUtil.unserialize(trainByte);
        //判断从redis中取出的数据是否为空 如果是则先从数据库中取出存入redis中
        if(goodsList==null){
            System.out.println("访问量数据库");
            goodsList = gd.queryGoods();
            strings.set("MYSCGoodsCache", SerializeUtil.serialize(goodsList));//序列化好的数据取名为serializeUserinfo存储到redis
        }
        return goodsList;
    }

    @Override
    public Goods getSingleGoods(int gid) {
        //现在取出
        byte[] trainByte = jedis.get(SafeEncoder.encode("MYSCGoodsCache"));
        //反序列化返回对象
        List goodsList = (List<Goods>) SerializeUtil.unserialize(trainByte);
        for (Object o : goodsList) {
            Goods goods = (Goods)o;
            if(goods.getGid()==gid){
                return goods;
            }
        }
        return null;
    }

    @Override
    public Integer getGoodsCount() {
        return gd.getGoodsCount();
    }

    @Override
    public List<Goods> getGoodsPageList(int page, int rows) {
        return gd.getGoodsPageList(page,rows);
    }

    @Override
    public boolean delGoods(Integer gid) {
        boolean flag = gd.delGoods(gid) > 0;
        reSave();
        return flag;
    }

    @Override
    public boolean updateGoods(Goods goods) {
        boolean flag = gd.updateGoods(goods) > 0;
        reSave();
        return flag;
    }

    @Override
    public boolean addGoods(Goods goods) {
        boolean flag = gd.addGoods(goods) > 0;
        reSave();
        return flag;
    }

    @Override
    public List<Goods> goodsLike(String gname, String lowernum, String uppernum,Integer page,Integer rows) {

        if(!"".equals(gname) && "".equals(lowernum) && "".equals(uppernum)){
            return gd.goodsLike(gname,null,null,page,rows);
        }

        if(!"".equals(gname) && !"".equals(lowernum) && !"".equals(uppernum)){
            return gd.goodsLike(gname,Integer.parseInt(lowernum),Integer.parseInt(uppernum),page,rows);
        }

        if("".equals(gname) && !"".equals(lowernum) && !"".equals(uppernum)){
            return gd.goodsLike(null,Integer.parseInt(lowernum),Integer.parseInt(uppernum),page,rows);
        }

        return gd.goodsLike(null,null,null,page,rows);
    }

    @Override
    public Integer getSearchCount() {
        return gd.getSearchCount();
    }


    @Override
    public boolean delAll(String ids) {
        boolean flag = gd.delAll(ids) > 0;
        reSave();
        return flag;
    }

    // 后台执行更新操作之后会同步到redis里面重新保存
    public void reSave(){
        List<Goods> allGoods = gd.queryGoods();
        strings.set("MYSCGoodsCache", SerializeUtil.serialize(allGoods));
    }
}
